Ext.ns('Tm.user');
Tm.user.UserRolePanel = Ext.extend(Ext.Panel, {
			id:'userRolePanel',
			userId: null,
			frame : false,
			autoWidth : true,
			layout : 'fit',
			initComponent : function() {
				var userId = this.userId;
				var tree = new Ext.tree.TreePanel({
					ref : 'tree',
					//useArrows : true,
					autoScroll : true,
					animate : true,
					rootVisible:false,
					//lines:true,
					//enableDD : true,
					containerScroll : true,
					checkModel : 'cascade', //对树的级联多选
					//onlyLeafCheckable : true,//只能选择树的叶子 
					loader : new Ext.tree.TreeLoader({
						dataUrl : 'user/loadRolesByUserId.do?userId='+userId
					// dataUrl:'extTest/jsp/bbb.json'
					})
				});
				tree.on('checkchange', function(node, checked) {
					node.expand();
					node.attributes.checked = checked;
					node.eachChild(function(child) {
						child.ui.toggleCheck(checked);
						child.attributes.checked = checked;
						child.fireEvent('checkchange', child, checked);
					});
				}, tree);

				// set the root node
				var root = new Ext.tree.AsyncTreeNode({
					text : '系统权限',
					draggable : false,
					id : 'source',
					checked:false

				});
				tree.setRootNode(root);
				
				var toolBar = new Ext.Toolbar({
							hideBorders : true,
							buttons : [ {
										text : '保存',
										iconCls : 'act_acceptIcon',

										handler : function() {
											var roles=[];
											var nodes=tree.getChecked();
											for(i in nodes){
												if(nodes[i]&&nodes[i].id){
													roles.push(nodes[i].id);
												}
											}

											Ext.Ajax.request({
												url : 'user/saveOrUpdateUserRole.do',
												params : {
													userId : userId,
													roles :roles 
												},
												success : function(response, action) {
													App.setAlert('ok', '操作成功');
												},
												failure : function(response, action) {
													App.setAlert('error', '操作失败');
												}

											});
											
										}
									}]
						});
				Ext.apply(this, {
							tbar : toolBar,
							items : [tree]
						});
				Tm.user.UserRolePanel.superclass.initComponent.call(this);
			},
			listeners : {
				render : function() {
					var userRolePanel = Ext.getCmp("userRolePanel");
					userRolePanel.tree.expandAll();
				}
			}
		});
